home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / smailsrc.zip / SMAIL.ZIP / MAIN.C < prev    next >
Text File  |  1990-05-05  |  7KB  |  280 lines

  1. /*
  2. **
  3. **  rmail/smail - UUCP mailer with automatic routing.
  4. **
  5. **  Christopher Seiwald        /+\
  6. **  chris@cbosgd.att.com    +\
  7. **  January, 1985        \+/
  8. **
  9. */
  10.  
  11. /*
  12. **    Patched for MS-DOS compatibility by Stephen Trier March, April and
  13. **    May, 1990.  This file is in the public domain.
  14. */
  15.  
  16. #ifndef lint
  17. static char     *sccsid="@(#)main.c    2.5 (smail) 9/15/87";
  18. #endif
  19.  
  20. /*
  21. **
  22. **  usage:      rmail [options] address...
  23. **        smail [options] address...
  24. **  options:
  25. **        -d         debug - verbose and don't invoke mailers.
  26. **        -v        verbose - just verbose.
  27. **        -A        print mapped addresses.  don't invoke mailers.
  28. **        -h hostname    set hostname 
  29. **        -H hostdomain    set hostdomain (default hostname.MYDOM)
  30. **        -p pathfile    path database filename
  31. **        -r        force routing of host!address
  32. **        -R        reroute even explicit path!user
  33. **        -l        user@domain goes to local mailer
  34. **        -L        all mail goes local
  35. **        -q number    mail queueing cost threshold
  36. **        -m number    limit on number of uux_noqueue jobs
  37. **        -u string    string of flags for uux
  38. **              -F address      name to substitute in From: line
  39. **        -a aliasfile    aliases filename (not used with SENDMAIL)
  40. **        -n namelist    list of full names for simple aliases
  41. */
  42.  
  43. #include    <stdio.h>
  44. #include    <ctype.h>
  45. #include    "defs.h"
  46. #ifdef MSDOS
  47. #include        <setjmp.h>
  48. #endif
  49.  
  50. int exitstat = 0;        /* exit status, set by resolve, deliver    */
  51.  
  52. enum edebug debug     = NO;    /* set by -d or -v option        */
  53. enum ehandle handle   = HANDLE;    /* which mail we can handle, see defs.h    */
  54. enum erouting routing = ROUTING;/* to route or not to route, see defs.h */
  55.  
  56. char hostname[SMLBUF]   = "";    /* set by -h, defaults in defs.h     */
  57. char hostdomain[SMLBUF] = "";    /* set by -H, defaults in defs.h     */
  58. char hostuucp[SMLBUF] = "";    /* built with hostname+".UUCP"         */
  59.  
  60. char *uuxargs   = NULL;         /* or set by -u                         */
  61.  
  62. #ifndef MSDOS
  63. char *pathfile  = PATHS;        /* or set by -p                         */
  64.  
  65. char *aliasfile =
  66. #ifdef ALIAS
  67.         ALIAS;        /* or set by -a                */
  68. #else
  69.         NULL;
  70. #endif
  71.  
  72. char *fnlist    =
  73. #ifdef FULLNAME
  74.         FULLNAME;    /* or set by -n                */
  75. #else
  76.         NULL;
  77. #endif
  78.  
  79. #else /* MSDOS */
  80. char *pathfile;
  81. char *aliasfile;
  82. char *fnlist;
  83. enum erouting routelevel;       /* Use for retrying failed addresses    */
  84. #endif  /* MSDOS */
  85.  
  86. int  queuecost  = QUEUECOST;    /* or set by -q                */
  87. char *from_addr = NULL;        /* or set by -F                */
  88. int  maxnoqueue = MAXNOQUEUE;    /* or set by -m                         */
  89.  
  90. int  getcost    = 
  91. #ifdef GETCOST
  92.         1;    /* get cost of path even if not routing */
  93. #else
  94.         0;
  95. #endif
  96.  
  97. char *spoolfile = NULL;        /* name of the file containing letter   */
  98. FILE *spoolfp;            /* file pointer to spoolfile        */
  99. int  spoolmaster = 0;        /* indicates 'control' of spoolfile     */
  100.  
  101. void spool();
  102.  
  103.  
  104. /*
  105. **
  106. **  rmail/smail: mail stdin letter to argv addresses.
  107. **
  108. **  After processing command line options and finding our host and domain 
  109. **  names, we map addresses into <host,user,form,cost> sets.  Then we deliver.
  110. **
  111. */
  112.  
  113. main(argc, argv)
  114. int argc;
  115. char *argv[];
  116. {
  117.     char *hostv[MAXARGS];        /* UUCP neighbor         */
  118.     char *userv[MAXARGS];        /* address given to host     */
  119.     int  costv[MAXARGS];        /* cost of resolved route    */
  120.     enum eform formv[MAXARGS];    /* invalid, local, or uucp     */
  121.     char *p;
  122.     int c;
  123.     int  printaddr  = 0;        /* or set by -A            */
  124.     int nargc;
  125.     char **nargv, **alias();
  126.  
  127.     char *optstr = "cdvArRlLH:h:p:u:q:a:n:m:f:F:";
  128.     extern char *optarg;
  129.     extern int optind;
  130.  
  131. #ifdef MSDOS
  132. /*
  133.  *  load variables from initialization files
  134.  */
  135.     config();
  136. #endif
  137.  
  138. /*
  139. **  see if we aren't invoked as rmail
  140. */
  141. #ifndef MSDOS
  142.     if((p = rindex(argv[0], '/')) == NULL) {
  143.         p = argv[0];
  144.     } else {
  145.         p++;
  146.     }
  147.  
  148.     if(*p != 'r' ) {
  149.         handle = ALL;
  150.     }
  151. #else  /* !MSDOS */
  152.     if (((p = rindex(argv[0], '/')) == NULL)
  153.         && ((p = rindex(argv[0], '\\')) == NULL))
  154.         p = argv[0];
  155.     else
  156.         p++;
  157.  
  158.     if ((*p != 'r' ) && (*p != 'R'))
  159.         handle = ALL;
  160. #endif  /* MSDOS */
  161.  
  162. /*
  163. **  Process command line arguments
  164. */
  165.     while ((c = getopt(argc, argv, optstr)) != EOF) {
  166.         switch ( c ) {
  167.         case 'd': debug      = YES;         break;
  168.         case 'v': debug      = VERBOSE;     break; 
  169.         case 'A': printaddr  = 1;         break; 
  170.         case 'F': from_addr  = optarg;        break;
  171. #ifndef MSDOS
  172.         case 'r': routing    = ALWAYS;          break;
  173.         case 'R': routing    = REROUTE;         break;
  174. #else
  175.         case 'r': routelevel = ALWAYS;          break;
  176.         case 'R': routelevel = REROUTE;         break;
  177. #endif
  178.         case 'l': handle     = JUSTUUCP;        break;
  179.         case 'L': handle     = NONE;        break;
  180.         case 'f': spoolfile  = optarg;        break;
  181.         case 'p': pathfile   = optarg;         break;
  182.         case 'u': uuxargs    = optarg;         break;
  183.         case 'a': aliasfile  = optarg;         break;
  184.         case 'n': fnlist     = optarg;         break;
  185.         case 'H': (void) strcpy(hostdomain, optarg);    break;
  186.         case 'h': (void) strcpy(hostname, optarg);     break;
  187.         case 'm': if(isdigit(*optarg)) {
  188.                   maxnoqueue = atoi(optarg);
  189.               }
  190.               break;
  191.         case 'c': getcost     = 1;        break;
  192.         case 'q': if(isdigit(*optarg)) {
  193.                   queuecost = atoi(optarg);
  194.               }
  195.               break;
  196.         default:
  197.             error( EX_USAGE, "valid flags are %s\n", optstr);
  198.         }
  199.     }
  200.     if ( argc <= optind ) {
  201.         error( EX_USAGE, "usage: %s [flags] address...\n", argv[0] );
  202.     }
  203.  
  204. /*
  205. **  Get our default hostname and hostdomain.
  206. */
  207.     getmynames();
  208.  
  209. /*
  210. **  Spool the letter in a temporary file.
  211. */
  212.     nargc = argc - optind;
  213.     if(printaddr == 0) {
  214.         spool(nargc, &argv[optind]);
  215.     }
  216.  
  217. /*
  218. ** Do aliasing and fullname resolution
  219. */
  220.     nargv = alias(&nargc, &argv[optind]);
  221.  
  222. #ifdef MSDOS
  223. /*
  224.  *  Prepare to accept a possible return to this location if a uux call fails.
  225.  */
  226.  
  227.     for (routing = routelevel; routing <= REROUTE; routing++)  {
  228.  
  229. #endif  /* MSDOS */
  230.  
  231. /*
  232. **  Map argv addresses to <host, user, form, cost>.
  233. */
  234.     map(nargc, nargv, hostv, userv, formv, costv);
  235. /*
  236. **  If all we want it mapped addresses, print them and exit.
  237. */
  238.     if(printaddr) {
  239.         int i;
  240.         char abuf[SMLBUF];
  241.         for(i=nargc-1; i >= 0; i--) {
  242.             if(formv[i] == ERROR) {
  243.                 (void) strcpy(abuf, nargv[i]);
  244.             } else {
  245.                 build(hostv[i], userv[i], formv[i], abuf);
  246.             }
  247.             (void) fputs(abuf, stdout);
  248.             if(i != 0) (void) putchar(' ');
  249.         }
  250.         (void) putchar('\n');
  251.         exit(0);
  252.     }
  253. /*
  254. **  Deliver.
  255. */
  256.     deliver(nargc, hostv, userv, formv, costv);
  257.  
  258. #ifdef MSDOS
  259. /*
  260.  *  Loop back for another try at the name resolution.
  261.  */
  262.     }
  263.  
  264. /*
  265.  *  Close and delete the spool file
  266.  */
  267.  
  268.     (void) fclose(spoolfp);
  269.     if (spoolmaster)  {
  270.     (void) unlink(spoolfile);
  271.     }
  272. #endif /* MSDOS */
  273.  
  274. /*
  275. **  Exitstat was set if any resolve or deliver failed, otherwise 0.
  276. */
  277.     return( exitstat );
  278.  
  279. }
  280.